package aco;

import java.util.Random;

public class Utilities {

    private static Random random;

    // [交换数组 v 和 v2 中的第i个元素和第j个元素的值] auxiliary routine for sorting an integer array
    static void swap2(double v[], int v2[], int i, int j) {
        double tmp1;
        int tmp2;
        tmp1 = v[i];
        v[i] = v[j];
        v[j] = tmp1;
        tmp2 = v2[i];
        v2[i] = v2[j];
        v2[j] = tmp2;
    }

    // recursive routine (quicksort) for sorting one array; second array does the same sequence of swaps
    // 根据v[]中元素的值，进行升序排列，同时对 v2[] 中元素的值进行相应更改
    static void sort2(double v[], int v2[], int left, int right) {
        int k, last;
        if (left >= right) return;

        // 取出 v[(left+right)/2]，将比它小的放在它左边，比它大的放在它右边
        swap2(v, v2, left, (left + right) / 2);
        last = left;
        for (k = left + 1; k <= right; k++) {
            if (v[k] < v[left]) {
                swap2(v, v2, ++last, k);
            }
        }
        swap2(v, v2, left, last);

        // 分治法，递归
        sort2(v, v2, left, last);
        sort2(v, v2, last + 1, right);
    }

    // generate a random number that is uniformly distributed in [0,1]
    static double random01() {
        if (random == null) {
            random = new Random();
        }
        return random.nextDouble();
    }
}